package com.blogs.service.web.impl;

import com.blogs.model.user.entity.user.UserEntity;
import com.blogs.model.user.entity.user.UserEntityDao;
import com.blogs.service.web.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xiaokai
 * @Description:
 * @Date: 2019/7/4
 * @Version: 1.0
 */
@Service
public class WebServiceImpl implements WebService {

    //注入springboot自动配置的redisTemplate
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private UserEntityDao userEntityDao;


    /**
     * Redis缓存无穿透---指第一次查询会从数据库查取，其后边全部的用户则从缓存获取信息
     */
    /*@Override
    public List<UserEntity> getAllUserMessage() {
        //字符串的序列化器
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);

        //高并发条件下，此处有点问题：缓存穿透
        //查询缓存
        List<UserEntity> userList = (List<UserEntity>) redisTemplate.opsForValue().get("users");
        //双重检测锁
        if (null == userList) {
            synchronized (this) {
                //从redis获取一下
                userList = (List<UserEntity>) redisTemplate.opsForValue().get("users");
                if (userList == null) {
                    System.out.println("查询数据库···············");
                    //缓存为空，判断数据库
                    userList = userEntityDao.findAll();
                    //把数据库中查到的数据，放入redis中
                    redisTemplate.opsForValue().set("users", userList);
                }else {
                    System.out.println("1查询的缓存···············");
                }
            }
        }else {
            System.out.println("2查询的缓存···············");
        }
        return userList;
    }*/

    /**
     * redis缓存穿透---首先部分用户会直接从数据库查取信息，后部分则从缓存中获取
     * @return
     */
    @Override
    public List<UserEntity> getAllUserMessage() {
        //字符串的序列化器
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);

        //高并发条件下，此处有点问题：缓存穿透
        //查询缓存
        List<UserEntity> userList = (List<UserEntity>) redisTemplate.opsForValue().get("users");
        //双重检测锁
        if (null == userList) {
                //从redis获取一下
                userList = (List<UserEntity>) redisTemplate.opsForValue().get("users");
                if (userList == null) {
                    System.out.println("查询数据库···············");
                    //缓存为空，判断数据库
                    userList = userEntityDao.findAll();
                    //把数据库中查到的数据，放入redis中
                    redisTemplate.opsForValue().set("users", userList);
            }
        }else {
            System.out.println("2查询的缓存···············");
        }
        return userList;
    }

    /**
     * 获取登录用户信息
     * @return
     */
    @Override
    public UserEntity seleLogin() {
        List<UserEntity> userEntity = userEntityDao.findAll();
        if (userEntity !=null){
            for (UserEntity userEntitys : userEntity){
                return userEntitys;
            }

        }
        return null;
    }

}
